package com.itheima.ydd.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.ydd.common.R;
import com.itheima.ydd.common.ResultData;
import com.itheima.ydd.dto.YddArticleDto;
import com.itheima.ydd.entity.YddArticle;
import com.itheima.ydd.entity.YddCate;
import com.itheima.ydd.service.IYddArticleService;
import com.itheima.ydd.service.IYddCateService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author author
 * @since 2022-05-22
 */
@RestController
@RequestMapping("/ydd_article")
@Slf4j
public class YddArticleController {
    @Autowired
    private IYddArticleService iYddArticleService;
    @Autowired
    private IYddCateService iYddCateService;
    //start_time=&end_time=&title=&cateid=&page=1&limit=10
    @GetMapping("/list")
    public R list(String start_time, String end_time,String title,String cateid,int page,int limit){
        Page<YddArticle> articlePage =new Page<>(page, limit);
        LambdaQueryWrapper<YddArticle> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.isNotEmpty(title),YddArticle::getTitle,"%"+title+"%");
        queryWrapper.eq(StringUtils.isNotEmpty(cateid),YddArticle::getCateid,cateid);
        if (start_time!=""||end_time!=""){
            queryWrapper.between(YddArticle::getCateid,Long.parseLong(start_time),Long.parseLong(end_time));
        }
        //根据创建时间排序
        queryWrapper.orderByDesc(YddArticle::getCreateDate);
        iYddArticleService.page(articlePage,queryWrapper);
        //传过来的时cateid  前端需要的是一个catename 需要对象拷贝
        Page<YddArticleDto> articleDtoPage=new Page<>();
        //数据不拷贝 ,因为需要对数据进行修改
        BeanUtils.copyProperties(articlePage,articleDtoPage,"records");
        //获取数据
        List<YddArticle> records = articlePage.getRecords();
        List<YddArticleDto> list = records.stream().map(item->{
            //创建dto把原来的records放进去
            YddArticleDto yddArticleDto=new YddArticleDto();
            BeanUtils.copyProperties(item,yddArticleDto);
            Integer cateId = item.getCateid();
            //根据分类id获取到yddcate对象
            YddCate yddcate = iYddCateService.getById(cateId);
            if (yddcate!=null){
                String catename = yddcate.getCatename();
                yddArticleDto.setCatename(catename);
                //传输createdate为null解决方法
                Long createDate = yddArticleDto.getCreateDate();
                yddArticleDto.setCreate_date(createDate.toString());


            }
            return yddArticleDto;
        }).collect(Collectors.toList());
        articleDtoPage.setRecords(list);


        R r = R.success("totalPages", (int)articleDtoPage.getPages());
        r.putData("pageSize", (int)articleDtoPage.getSize());
        r.putData("currentPage", (int)articleDtoPage.getCurrent());
        r.putData("count", (int)articleDtoPage.getTotal());
        r.putData("data", articleDtoPage.getRecords());
        return r;
    }
    /*
    * 删除
    * */
    @GetMapping("/del")
    public R del(Long id){
    iYddArticleService.removeById(id);
    R r=new R();
    r.setErrno(0);
    return r;
    }

    /*
    数据回显
     */

    @GetMapping("/rowInfo")
    public ResultData rowInfo(Long id){
        LambdaQueryWrapper<YddArticle> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(YddArticle::getId,id);

        YddArticle yddArticle = iYddArticleService.getOne(queryWrapper);
        //将毫秒值对象 转换成Date类型
        YddArticleDto dto=new YddArticleDto();
        BeanUtils.copyProperties(yddArticle,dto);
        Long createDate = yddArticle.getCreateDate();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String format = simpleDateFormat.format(createDate);

        dto.setCreate_date(format);
        log.info("{}",dto);
        log.info("文章{}",yddArticle);
        return ResultData.ok("",dto);
    }
    /*
    * 添加
    * */
    @PostMapping("add")
    public ResultData add(@RequestBody YddArticle yddArticle){
        boolean save = iYddArticleService.save(yddArticle);
        log.info("--------==========={}",save);
        return ResultData.ok("",yddArticle);
    }
    /*
    * 修改
    * */
    @PostMapping("update")
    public ResultData update(@RequestBody YddArticle yddArticle){
        iYddArticleService.updateById(yddArticle);
        return ResultData.ok("",yddArticle);

    }




}
